package com.lin.controller;

import com.lin.entity.Users;
import com.lin.service.UsersService;
import com.lin.vo.LoginVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
public class LoginController {

    @Resource
    private UsersService usersService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }


    @RequestMapping("/login")
    public String login(@Valid LoginVO loginVO, Model model, HttpServletRequest request){
        Users users = usersService.selLogin(loginVO);
        if (users==null){
            model.addAttribute("error","用户不存在");
            return "login";
        }
        if (!loginVO.getUserPassword().equals(users.getPassword())){
            model.addAttribute("error","密码错误");
            return "login";
        }
        request.getSession().setAttribute("users",users);
        request.getSession().setAttribute("name",loginVO.getUserCode());
        return "redirect:/selAll";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("users");
        request.getSession().removeAttribute("name");
        return "login";
    }
}
